$(function (){
    //订单条目渲染
    let orderInfo = sessionStorage.getItem("shopping.orderInfo");
    let order = JSON.parse(orderInfo);
    //渲染订单编号
    $(".order-list-footer-oid").text(`订单编号：${order.id}`);
    //渲染总价格
    $(".price-sum em").text(`￥${order.account}`);
    //渲染商品条目
    let itemsList = order.items;
    let item = ``;
    for(let i = 0 ; i < itemsList.length ; i++){
        item += `
                    <div class="order-item d-flex mt-2">
                        <div class="d-flex order-item-details col-6">
                            <img src="${itemsList[i].product.pimage}" alt="">
                            <div class="mx-4 mt-5">${itemsList[i].product.name}</div>
                        </div>
                        <div class="order-item-size col-2 text-center mt-5">${itemsList[i].size}码</div>
                        <div class="order-item-price col-2 text-center mt-5">￥${itemsList[i].shoppingPrice}</div>
                        <div class="order-item-count col-2 text-center mt-5">X${itemsList[i].qty}</div>
                    </div>
            `;
        // 删除购物车商品
        fetch(`remove.car?pid=${itemsList[i].product.id}&size=${itemsList[i].size}`).then(response=>response.json()).then(data=>{
            if(data.flag){
                console.log(data);
            }
        })
    }
    $("#items-list").html(`${item}`);
})
$("#payment").on("click",function (){
    let receiver = $("#order-step-name").val();
    let tel = $("#order-step-phone").val();
    let address = $("#order-step-detailedAddress").val();
    let province = document.querySelector("#province");
    let city = document.querySelector("#city");
    let area = document.querySelector("#area");
    address=provinceList[province.selectedIndex-1].name+"省"+provinceList[province.selectedIndex-1].cityList[city.selectedIndex].name+provinceList[province.selectedIndex-1].cityList[city.selectedIndex].areaList[area.selectedIndex]+address;

    let orderInfo = sessionStorage.getItem("shopping.orderInfo");
    let order = JSON.parse(orderInfo);
    let oid = order.id;

    let userInfo = sessionStorage.getItem("shopping.userInfo");
    let user = JSON.parse(userInfo);
    let uid = user.id;

    let requestObj ={
        "oid": oid,
        "address": {"address": address, "receiver": receiver, "tel": tel, "user": {"id": uid}}
    };

    fetch(`pay.order`, {
        method: "POST",
        contentType: "application/json",
        body:JSON.stringify(requestObj)
    }).then(response=>response.json()).then(data=>{

    });
    location.href = "payment.html";

})